import axios from 'axios';

// 创建 axios 实例
const request2 = axios.create({
  baseURL: '/api2', // 后端接口的根路径
  timeout: 5000,  // 设置请求超时
});

// 请求拦截器 - 在请求发送之前可以做一些处理
request2.interceptors.request.use(
  config => {
    // 在请求头中添加 Cookie 或其他必要的内容
    config.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01';
    config.headers['Accept-Language'] = 'zh-CN,zh;q=0.9';
    config.headers['Connection'] = 'keep-alive';
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
    config.headers['Origin'] = 'https://jxgl.wyu.edu.cn';
    config.headers['Referer'] = 'https://jxgl.wyu.edu.cn/new/student/xsgrkb/week.page?xnxqdm=202401';
    config.headers['X-Requested-With'] = 'XMLHttpRequest';
    config.headers['sec-ch-ua'] = '"Chromium";v="9", "Not?A_Brand";v="8"';
    config.headers['sec-ch-ua-mobile'] = '?1';
    config.headers['sec-ch-ua-platform'] = '"Android"';
    config.headers['Cookie'] = 'JSESSIONID=D2F18F7981C229D99F257585952CFC3F; wzws_sessionid=gjkxODRhNaBnWa7TgDExOS4xMzMuNS4xMjOBZTJkOWZi';
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 封装POST请求
export const postRequest = async (url, data) => {
  try {
    const response = await request2.post(url, new URLSearchParams(data)); // 使用 URLSearchParams 来格式化数据
    return response.data;
  } catch (error) {
    console.error('Error in postRequest:', error);
    throw error;
  }
};
